package com.feihong.txgw.adapter.order;

import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.adapter.base.BaseQuickAdapter;
import com.adapter.base.BaseViewHolder;
import com.adapter.base.animation.ScaleInAnimation;
import com.bumptech.glide.Glide;
import com.feihong.txgw.R;
import com.feihong.txgw.json.user.AllOrderInfo;
import com.feihong.txgw.ui.activity.order.PayActivity;

/**
 * @Author wangziheng
 * @ProjectName txgw_app
 * @ClassName OrderAdapter
 * @package com.feihong.txgw.adapter.order
 * @data 2018/6/6 10:12
 * @Describe
 * @Change
 */
public class OrderAdapter extends BaseQuickAdapter<AllOrderInfo.DataBean.OrderPageBean,BaseViewHolder> {
    private TextView
            //订单名称
            mHfcz,
            // 订单状态
            mDfk,
            // 订单类型
            mType,
            // 暂无用处
            mMgj,
            mQwt,
            // 开卡订单  送货方式
            mGb,
            // 取消删除按钮
            mCancle,
            // 确认订单  支付  扫卡按钮
            mQuren;
    private ImageView
            //订单图标
            mImgIcon;

    public OrderAdapter(int layoutResId) {
        super(layoutResId);
        openLoadAnimation(new ScaleInAnimation(0.8f));
        isFirstOnly(false);
    }

    /**
     *
     * @param position
     * @return  返回值代表 数据是否删除完毕  用于表示 是否展示空数据界面
     */
    public boolean removeItem(int position){
        remove(position);
        int count = mData.size();
        if(count == 0){
            return true;
        }
        return false;
    }
    @Override
    protected void convert(BaseViewHolder helper, AllOrderInfo.DataBean.OrderPageBean item) {
        helper.setText(R.id.hfcz,item.getBuyer_id());
        helper.setText(R.id.dfk,item.getBuyer_id());
        helper.setText(R.id.type,item.getBuyer_id());
        helper.setText(R.id.gb,item.getBuyer_id());
        helper.setText(R.id.cancle,item.getBuyer_id());
        helper.setText(R.id.quren,item.getBuyer_id());

        helper.addOnClickListener(R.id.cancle);
        helper.addOnClickListener(R.id.quren);

        helper.setText(R.id.phone,item.getPhonenum());
        helper.setText(R.id.money,"¥ " + item.getPrice());
        helper.setText(R.id.time,item.getAdd_time());

        mHfcz = helper.getView(R.id.hfcz);
        mDfk = helper.getView(R.id.dfk);
        mType = helper.getView(R.id.type);
        mGb = helper.getView(R.id.gb);
        mImgIcon = helper.getView(R.id.img_icon);
        mCancle = helper.getView(R.id.cancle);
        mQuren = helper.getView(R.id.quren);
        getTabID(helper,item);
    }

    private int tableID,status;
    private String orderSn;
    /**
     * 是否可以评论
     * 0 待评价状态 可以评价
     * 1 已评价状态  不可以评价
     */
    private int isReViewConmment = 0;

    /**
     * 根据订单种类 进行数据分配
     * @param helper
     * @param item
     */
    private void getTabID(BaseViewHolder helper, AllOrderInfo.DataBean.OrderPageBean item) {
        tableID = Integer.valueOf(item.getTable_id());
        status = Integer.valueOf(item.getStatus());
        orderSn = item.getOrder_sn();
        if(!TextUtils.isEmpty(item.getComment_status())){
            isReViewConmment = Integer.valueOf(item.getComment_status());
        }

        switch (tableID){
            case PayActivity.PAY_TYPE_PHONE_RECHARGE:
                mHfcz.setText("话费充值 (订单号:" + orderSn + ")");
                mGb.setVisibility(View.GONE);
                Glide.with(mContext).load(R.mipmap.huafei_ico).into(mImgIcon);
                getIcpID(item);
                rechargeType(status);
                break;
            case PayActivity.PAY_TYPE_PHONE_GPRS:
                mHfcz.setText("流量充值 (订单号:" + orderSn + ")");
                mGb.setVisibility(View.GONE);
                Glide.with(mContext).load(R.mipmap.liuliang_ico).into(mImgIcon);
                getIcpID(item);
                rechargeType(status);
                break;
            case PayActivity.PAY_TYPE_KAIKA:
                mHfcz.setText("手机开卡 (订单号:" + orderSn + ")");
                if ("2".equals(item.getOrder_type())) {
                    mGb.setVisibility(View.VISIBLE);
                    if ("1".equals(item.getDelivery_type())) {
                        mGb.setText("寄卡");
                    } else {
                        mGb.setText("附近门店");
                    }
                } else {
                    mGb.setVisibility(View.GONE);
                }
                Glide.with(mContext).load(R.mipmap.kaika_ico).into(mImgIcon);
                getIcpID(item);
                kaiKaType(item);
                break;
            case PayActivity.PAY_TYPE_PRODUCT:
                mHfcz.setText("终端商品 (订单号:" + orderSn + ")");
                helper.setText(R.id.phone,item.getSku_name());
                mGb.setVisibility(View.GONE);
                Glide.with(mContext).load(R.mipmap.zhongduan_ico).into(mImgIcon);
                zhongDuanType(status);
                break;
            default:break;
        }
    }

    /**
     * 终端类型
     *
     * @param status
     */
    private void zhongDuanType(int status) {
        switch (status) {
            case 1:
                mDfk.setText("待支付");
                mCancle.setText("取消订单");
                mQuren.setText("确认支付");
                mQuren.setVisibility(View.VISIBLE);
                mCancle.setVisibility(View.VISIBLE);
                break;
            case 2:
                mDfk.setText("已付款");
                mCancle.setVisibility(View.GONE);
                mQuren.setVisibility(View.VISIBLE);
                mQuren.setText("等待发货");
                break;
            case 3:
                mDfk.setText("已发货");
                mCancle.setVisibility(View.GONE);
                mQuren.setVisibility(View.VISIBLE);
                mQuren.setText("确认收货");
                break;
            case 4:
                if (isReViewConmment == 0) {
                    mQuren.setVisibility(View.VISIBLE);
                    mQuren.setText("评价");
                } else {
                    mQuren.setVisibility(View.GONE);
                }
                mDfk.setText("已评价");
                mCancle.setVisibility(View.GONE);

                break;
            case 5:
                mDfk.setText("已取消");
                mCancle.setVisibility(View.GONE);
                mQuren.setVisibility(View.VISIBLE);
                mQuren.setText("删除订单");
                break;
            case 7:
                mDfk.setText("退款中");
                mCancle.setVisibility(View.GONE);
                mQuren.setVisibility(View.GONE);
                break;
            case 6:
                mDfk.setText("已失败");
                mCancle.setVisibility(View.GONE);
                mQuren.setVisibility(View.GONE);
                break;
            default:
                break;
        }
    }

    /**
     * 充值类型
     * @param status
     */
    private void rechargeType(int status) {
        switch (status) {
            case 0:
                mDfk.setText("待支付");
                mCancle.setText("取消订单");
                mQuren.setText("支付订单");
                mQuren.setVisibility(View.VISIBLE);
                mCancle.setVisibility(View.VISIBLE);
                break;
            case 1:
                mDfk.setText("已受理");
                mQuren.setText("已受理");
                mQuren.setVisibility(View.VISIBLE);
                mCancle.setVisibility(View.GONE);
                break;
            case 2:
                mDfk.setText("已失败");
                mCancle.setVisibility(View.GONE);
                mQuren.setVisibility(View.GONE);
                break;
            case 3:
                mDfk.setText("处理中");
                mCancle.setVisibility(View.GONE);
                mQuren.setVisibility(View.GONE);
                break;
            case 4:
                // 充值的完成订单 没有评论
                mDfk.setText("已完成");
                mQuren.setVisibility(View.GONE);
                mQuren.setVisibility(View.VISIBLE);
                mQuren.setText("删除订单");
                break;
            case 5:
                mDfk.setText("已取消");
                mCancle.setVisibility(View.GONE);
                mQuren.setVisibility(View.VISIBLE);
                mQuren.setText("删除订单");
                break;
            case 6:
                mDfk.setText("退款中");
                mCancle.setVisibility(View.GONE);
                mQuren.setVisibility(View.GONE);
                break;
            default:
                break;
        }
    }


    /**
     * 开卡类型
     * <p>
     * //     * @param htjcId    活体检测id
     * //     * @param orderType 订单类型   1  普通订单  2 预约订单
     * //     * @param status
     */
    private void kaiKaType(AllOrderInfo.DataBean.OrderPageBean item) {
        if ("2".equals(item.getOrder_type())) {
            int status = Integer.valueOf(item.getStatus());
            switch (status) {
                case 0:
                    mCancle.setText("取消订单");
                    mQuren.setText("支付订单");
                    mDfk.setText("未支付");
                    break;
                case 1:
                    mDfk.setText("已支付");
                    mCancle.setVisibility(View.GONE);
                    mQuren.setText("扫卡开户");
                    if ("0".equals(item.getIs_fh())) {
                        mDfk.setText("未发货");
                    }
                    break;
                case 2:
                    mDfk.setText("正处理");
                    mCancle.setVisibility(View.GONE);
                    mQuren.setText("正处理");
                    break;
                case 3:
                    mDfk.setText("已完成");
                    mCancle.setVisibility(View.GONE);
                    mQuren.setVisibility(View.GONE);
                    break;
                case 4:
                    mDfk.setText("已取消");
                    mCancle.setVisibility(View.GONE);
                    mQuren.setVisibility(View.VISIBLE);
                    mQuren.setText("删除订单");
                    break;
                case 5:
                    mDfk.setText("退款中");
                    mCancle.setVisibility(View.GONE);
                    mQuren.setVisibility(View.GONE);
                    break;
                case 6:
                    mDfk.setText("已失败");
                    mCancle.setVisibility(View.GONE);
                    mQuren.setVisibility(View.GONE);
                    break;
                default:
                    break;
            }
        } else {
            switch (status) {
                case 0:
                    mDfk.setText("待支付");
                    mCancle.setText("取消订单");
                    mQuren.setText("支付订单");
                    break;
                case 1:
                    mDfk.setText("已支付");
                    mCancle.setVisibility(View.GONE);
                    mQuren.setText("已受理");
                    break;
                case 2:
                    mDfk.setText("正处理");
                    mCancle.setVisibility(View.GONE);
                    mCancle.setText("正处理");
                    break;
                case 3:
                    mDfk.setText("已完成");
                    mCancle.setVisibility(View.GONE);
                    mQuren.setVisibility(View.GONE);
                    break;
                case 4:
                    mDfk.setText("已取消");
                    mCancle.setVisibility(View.GONE);
                    mQuren.setVisibility(View.VISIBLE);
                    mQuren.setText("删除订单");
                    break;
                case 5:
                    mDfk.setText("退款中");
                    mCancle.setVisibility(View.GONE);
                    mQuren.setVisibility(View.GONE);
                    break;
                case 6:
                    mDfk.setText("已失败");
                    mCancle.setVisibility(View.GONE);
                    mQuren.setVisibility(View.GONE);
                    break;
                default:
                    break;
            }
        }

    }

    private void getIcpID(AllOrderInfo.DataBean.OrderPageBean item) {
        mType.setVisibility(View.VISIBLE);
        int icpID = Integer.parseInt(item.getIcp_id());
        switch (icpID) {
            case 1:
                mType.setText("(联通)");
                break;
            case 2:
                mType.setText("(移动)");
                break;
            case 3:
                mType.setText("(电信)");
                break;
            default:
                break;
        }
    }
}
